package com.aigc.sclassroom.mapper;

import com.aigc.sclassroom.pojo.Course;
import com.aigc.sclassroom.pojo.StuCourse;
import com.aigc.sclassroom.pojo.Team;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface CourseMapper {
    @Select("SELECT DISTINCT cm.course_manage_id,\n" +
            "     c.course_name,\n" +
            "        su.user_id,\n" +
            "        cm.period,\n" +
            "        cr.classroom_name,\n" +
            "        cm.day\n" +
            "        FROM course_manage cm\n" +
            "        JOIN sys_user su ON cm.user_id = su.user_id\n" +
            "        JOIN course c ON cm.course_id = c.course_id\n" +
            "        JOIN calendar ca ON cm.calendar_id = ca.calendar_id\n" +
            "        JOIN classroom cr ON cm.classroom_id = cr.classroom_id\n" +
            "where cm.user_id =#{userId};")
    List<Course> findByUserId(Long userId);

    @Select("select t.course_manage_id,c.course_name,su.user_name,cm.period,c2.classroom_name,cm.day\n" +
            "from team t\n" +
            "left join course_manage cm on t.course_manage_id =cm.course_manage_id\n" +
            "left join course c on cm.course_id = c.course_id\n" +
            "left join sys_user su on cm.user_id = su.user_id\n" +
            "left join classroom c2 on cm.classroom_id = c2.classroom_id\n" +
            "where team_user_id = #{userId};")
    List<StuCourse> findByTeamUserId(Long userId);

    /**
     * 查询课程下的学生----班级里的学生
     * @param courseManageId
     * @return
     */

    @Select("SELECT DISTINCT cm.course_manage_id,\n" +
            "     c.course_name,\n" +
            "        su.user_id,\n" +
            "        ca.period,\n" +
            "        cr.classroom_name\n" +
            "        FROM course_manage cm\n" +
            "        JOIN sys_user su ON cm.user_id = su.user_id\n" +
            "        JOIN course c ON cm.course_id = c.course_id\n" +
            "        JOIN calendar ca ON cm.calendar_id = ca.calendar_id\n" +
            "        JOIN classroom cr ON cm.classroom_id = cr.classroom_id\n" +
            "where cm.course_manage_id =#{courseManageId}")
    Course getCourseDetail(Long courseManageId);


    @Select("select t.team_id,t.team_user_id,cm.course_manage_id,su.user_name,su.nick_name,su.email\n" +
            "from team t\n" +
            "left join sys_user su on t.team_user_id = su.user_id\n" +
            "left join course_manage cm on t.course_manage_id = cm.course_manage_id\n" +
            "left join course c on cm.course_id = c.course_id\n" +
            "where t.course_manage_id=#{courseManageId}")
    List<Team> findByCourseManageId(Long courseManageId);

    @Insert("insert into team(team_user_id,course_manage_id)" +
            "values (#{teamUserId},#{courseManageId})")
    void addStudent(Long teamUserId,Long courseManageId);

    @Delete("delete from team where team_id=#{teamId}")
    void deleteByTeamId(Long teamId);
}
